import java.util.Scanner;

public class MaxSubarraySum{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入整数数组（元素用空格或逗号分隔，例如：1 -2 3 5 -1）：");
        String input = scanner.nextLine().trim();
        scanner.close();
        int[] arr;
        try {
            String[] strElements = input.replace(",", " ").split("\\s+");
            arr = new int[strElements.length];
            for (int i = 0; i < strElements.length; i++) {
                arr[i] = Integer.parseInt(strElements[i]);
            }
        } catch (NumberFormatException e) {
            System.err.println("输入错误！请仅输入整数，并用空格或逗号分隔。");
            return; // 输入非法时退出
        } catch (ArrayIndexOutOfBoundsException e) {
            System.err.println("输入不能为空！请至少输入一个整数。");
            return;
        }
        int maxSum = calculateMaxSubarraySum(arr);
        System.out.println("该数组中子数组之和的最大值为：" + maxSum);
    }
    private static int calculateMaxSubarraySum(int[] arr) {
        if (arr.length == 1) {
            return arr[0];
        }
        int currentMax = arr[0]; // 以当前元素结尾的子数组的最大和
        int globalMax = arr[0];  // 全局子数组的最大和
        for (int i = 1; i < arr.length; i++) {
            currentMax = Math.max(arr[i], currentMax + arr[i]);
            if (currentMax > globalMax) {
                globalMax = currentMax;
            }
        }
        return globalMax;
    }
}